const router = require('koa-router')()
const cc = require('../conCtl')
const js = require('../jsCtl')
const db = require('../dbCtl')
const { now } = require('moment')

/**
@hostpath = http://127.0.0.1/service/moneySet
 */

/** 获取积分兑换设置列表
###
    GET {{hostpath}}?page=1&size=30
### */
router.get('/', async c => {
  let
    page = c.query.page || 1,
    size = c.query.size || 30;

  let model = db.model('t_money_set');

  let data = await model.orderBy('dt desc').select(page, size);
  let count = await model.count();

  cc.send_list(c, data, count);
})

/** 新增积分兑换
###
    POST {{hostpath}}/add
    Content-Type: application/json

    {
        "score":11,
        "money":55
    }
### */
router.post('/add', async c => {
  js.check_params(
    !c.request.body.score && '?积分' ||
    !c.request.body.money && '?金额');

    await db.model('t_money_set').insert({score:c.request.body.score,money:c.request.body.money});
  cc.send(c);
})

/** 修改积分
###
    POST {{hostpath}}/score
    Content-Type: application/json

    {
        "money_id":1,
        "score":11
    }
### */
router.post('/score', async c => {
  js.check_params(
    !c.request.body.money_id && '!金额兑换列表ID' ||
    !c.request.body.score && '?积分');

  await db.model('t_money_set').where('money_id=?', c.request.body.money_id).update({ score: c.request.body.score });
  cc.send(c);
})

/** 修改金额
###
    POST {{hostpath}}/money
    Content-Type: application/json

    {
        "money_id":1,
        "money":11
    }
### */
router.post('/money', async c => {
  js.check_params(
    !c.request.body.money_id && '!金额兑换列表ID' ||
    !c.request.body.money && '?金额');

  await db.model('t_money_set').where('money_id=?', c.request.body.money_id).update({ money: c.request.body.money });
  cc.send(c);
})

/** 删除积分兑换列表
###
    DEL {{hostpath}}
### */
router.del('/', async c => {
  js.check_params(!c.request.body.money_id && '!金额兑换列表ID');

  await db.model('t_money_set').where('money_id=?', c.request.body.money_id).delete();

  cc.send(c);
});




module.exports = router